import { KoaCtx, IServers } from "../interface/IFace";

export default async function config(ctx:KoaCtx,next:Function){        
    try {
        let host = ctx.host;
        let url = ctx.request.url;
        if (this._reserve[host]) {
            let config:IServers = this._reserve[host];
            let match = url.match(/\/([A-Za-z][A-Za-z0-9]{0,})\/([A-Za-z][A-Za-z0-9]{0,})/)
            let Controller:string='';
            let Method:string='';
            if (match !== null) {
                Controller = match[1]
                Method = match[2]
            }
            config.Controller = Controller;
            config.Method = Method;
            ctx.config = config;
            if(config.permission instanceof Function  && await config.permission(ctx,{Controller,Method},next)){
                
            }
            //自动加载解码相关的函数逻辑
            if (config.decode instanceof Function) {
                await config.decode.apply(ctx, [ctx])
            }
        }
    }catch(e){

    }
}